package com.zhumr.admin.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.zhumr.model.User;
import com.zhumr.service.IUserService;

@Controller
@RequestMapping("/admin")
public class AdminController extends UpLoaderController {

	@Autowired
	private IUserService userService;

	@RequestMapping(value = "/hello", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public String sayHello(HttpServletRequest request, String name) throws IOException {

		return "hi";
	}

	/**
	 * 返回视图
	 */
	// @RequestMapping(value = "/hello", method = { RequestMethod.GET,
	// RequestMethod.POST })
	// public ModelAndView hello(HttpServletRequest request, String name) throws
	// IOException {
	//
	// User user = new User();
	// // user.setName(name);
	//
	// ModelAndView resultAndView = new ModelAndView("hello.html");
	// resultAndView.addObject("user", user);
	//
	// return resultAndView;
	// }

	/**
	 * 返回json
	 */
	// @RequestMapping(value = "/login", method = { RequestMethod.GET,
	// RequestMethod.POST })
	// @ResponseBody
	// public Map<String, Object> getUser(HttpServletRequest request, String
	// name, String password) throws IOException {
	// User user = new User();
	// user.setUserName(name);
	// user.setPasswordN(password);
	//
	// Map<String, Object> result = userService.selectUserByName(user);
	//
	// return result;
	// }

	// 文件上传
	@RequestMapping(value = "/uploaderfile")
	public String uploaderfile(MultipartFile file, HttpServletRequest request, HttpServletResponse response) {
		System.out.println("in..");
		try {
			super.upload(file, "/static_resources/img/uploader/", request);
			response.getWriter().print(super.getFileName());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	// 分页数据
	@RequestMapping(value = "/data", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> getData() {
		Map<String, Object> map = new HashMap<String, Object>();
		List<User> list = new ArrayList<User>();
		for (int i = 1; i <= 50; i++) {
			User u = new User();
			// u.setName("tom" + i);
			list.add(u);
		}
		map.put("users", list);
		return map;
	}

}
